#!/usr/bin/perl

use strict;
use DBI;
use CGI;
use TTraker;

my $dbh;
initialize_dbi();
print_header();

my $form_ref = parse_input();
my %FORM_DATA = %$form_ref; 
my $supNum = $FORM_DATA{'supNum'};
my $supName = $FORM_DATA{'supName'};
my $supJob = $FORM_DATA{'supJob'};
my $supLoc = $FORM_DATA{'supLoc'};
my $empNum = $FORM_DATA{'empNum'};
my $empName = $FORM_DATA{'empName'};
my $empFac = $FORM_DATA{'empFac'};
my $payDate = $FORM_DATA{'payDate'};
my $cleanDirty = $FORM_DATA{'cleanDirty'};

my %Cli=();
my %Doc=();
my @list = ();
my $max = 0;
my $min = 10000000;
my %Cli=();
my %Doc=();
foreach my $key (keys %FORM_DATA) 
{
  if ($key =~ /^CLI_/ ) {
    my $id = $key;
    $id =~ s/CLI_//;
    my $cli = $FORM_DATA{$key};
    if ($cli ne "NUL" ) {
    	if ($id > $max ) {
    		$max = $id;
    	}
 		if ($id < $min ) {
    		$min = $id;
		}
   }	
   $Cli{$id}="$cli";
   push (@list, $id);
  }
  if ($key =~ /^DOC_/ ) {
    my $id = $key;
    $id =~ s/DOC_//;
    my $doc = $FORM_DATA{$key};
	 $Doc{$id}=$doc;		
  }
}
my @List = sort @list;
my ($curCli, $curDoc);
my @updates=();
foreach my $Id ( @List) {
	if ( $Id >= $min && $Id <= $max ) {
		if ($Cli{$Id} ne "NUL" ) {
			$curCli = $Cli{$Id};
			$curDoc = $Doc{$Id};
		} else {
			$Cli{$Id} = $curCli;
			$Doc{$Id} = $curDoc;
		}	
	}
		my @tmp = ($Id, $Doc{$Id}, $Cli{$Id});
#		print "UPDATE $Id, $Doc{$Id}, $Cli{$Id}<br/>";
		push @updates, [@tmp];
			
}


update_clean_tc_tmp_table ( \@updates, $supNum);

# update_clean_tc_tmp_table_PLACE_HOLDERS ( \@updates, $supNum);

redraw_time_card_page ($supNum, $supName, $supJob, $supLoc, $empNum, $payDate, $empName, $empFac);

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub redraw_time_card_page
{
	my $supNum = $_[0];
	my $supName = $_[1];
	my $supJob = $_[2];
	my $supLoc = $_[3];
	my $empNum = $_[4];
	my $payDate = $_[5];
	my $empName = $_[6];
	my $empFac = $_[7];
	
	my @tcData = ();
	my $tcData = get_time_card_data ($supNum);
	@tcData = @$tcData;
	my $client_ref = get_time_card_clients($empFac); 
	my @clients = @$client_ref;

  print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_sup_time_card_data</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  <div id='box5_1'>
  <h3>Time Card: $empName : $payDate : Facility # $empFac</h3>
  <form onkeypress='return event.keyCode != 13;' autocomplete='off'  name='activity' action='${cgi_ref}trak_04_sup_time_card_refresh.pl' method='post'>
  <input type="hidden" name="supNum" value='$supNum'/>
  <input type="hidden" name="supName" value='$supName'/>
  <input type="hidden" name="supJob" value='$supJob'/>
  <input type="hidden" name="supLoc" value='$supLoc'/>
  <input type="hidden" name="empNum" value='$empNum'/>
  <input type="hidden" name="empName" value='$empName'/>
  <input type="hidden" name="empFac" value='$empFac'/>
  <input type="hidden" name="payDate" value='$payDate'/>
  <input type="hidden" name="cleanDirty" value='$cleanDirty'/>

  <table id="small">
    <tr>
      <th>Time</th>
      <th>Doc No. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>
      <th>Client</th>
    </tr>
ENDHEADER
 # print "THERE ARE $#tasks TASKS!<br/>"; 
 for my $i (0..$#tcData ) {
		my $id = $tcData[$i][0];		
		my $tag = $tcData[$i][1];	
		my $date = $tcData[$i][2];
		my $num = $tcData[$i][3];
		my $start = $tcData[$i][4];
		my $end = $tcData[$i][5];
		my $doc = $tcData[$i][6];
		my $cli = $tcData[$i][7];
		my $ti = $i;
#			print "$id, $tag, $date, $num, $start, $end, $doc, $cli <br/>";
			print <<ENDHEADER;
			<tr>
				<td align=left>$tag &nbsp&nbsp</td>
				<td align=left><input type="text" size="9" value="$doc" name="DOC_$id"  /></td>
				<td> <select id=clientDrop name="CLI_$id" tabindex="2" >
ENDHEADER
#			print "$optStr";
			# add a blank element at the start of the list
			print "<option value='NUL'></option>";
			# add all clients on the list to the drop down
			foreach my $Cli (@clients) {
				if ( $Cli eq $cli ) {
		  			print "<option value=$Cli selected='selected' >$Cli</option>";
				} else {					
	  				print "<option value=$Cli >$Cli</option>";
	  			}	
			}
			print <<ENDHEADER;
				</td>
			</tr>
ENDHEADER
 }
 print <<ENDHEADER;
 </table>
 <br/><br/>
 <input type='submit' value='Refresh Time Card Page'>
 <br/><br/>
 <p id="warn">Refresh any changes before committing.</p>
ENDHEADER
 if ( $cleanDirty eq "CLEAN" ) {
 	print "<a href='${cgi_ref}trak_05_sup_commit_clean_tc.pl?supNum=$supNum&supName=$supName&supJob=$supJob&supLoc=$supLoc&empNum=$empNum&empName=$empName&payDate=$payDate&empFac=	$empFac&cleanDirty=$cleanDirty'><span><b>Commit Data to DBase</b></span></a><br/>";
 } else {
 	print "<a href='${cgi_ref}trak_05_sup_commit_dirty_tc.pl?supNum=$supNum&supName=$supName&supJob=$supJob&supLoc=$supLoc&empNum=$empNum&empName=$empName&payDate=$payDate&empFac=$empFac&cleanDirty=$cleanDirty'><span><b>Commit Data to DBase</b></span></a><br/>";
 }
 print <<ENDHEADER;
 <br/><br/>
 <a href="${cgi_ref}trak_00_sup_activity.pl?num=$supNum&name=$supName&job=$supJob&loc=$supLoc">Return to Activities</a>
 <br/><br/>
 </form>
 </body>
 </html>
ENDHEADER

}



